package cn.starremote.component.utils.encrypt;

import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.util.encoders.Hex;

import java.math.BigInteger;
import java.security.KeyPair;

import static org.junit.jupiter.api.Assertions.*;

class SM2UtilTest {

    @org.junit.jupiter.api.Test
    void signSm3WithSm2() {
        // 生成公私钥对 ---------------------
        KeyPair kp = SM2Util.generateKeyPair();
        String privateKeyStr = Hex.toHexString(kp.getPrivate().getEncoded());
        String pubKey =Hex.toHexString(kp.getPublic().getEncoded());

        System.out.println("PrivateKey="+ privateKeyStr);
        System.out.println("pubKey="+pubKey);

        System.out.println(kp.getPrivate().getFormat());
        System.out.println(kp.getPublic().getFormat());
        System.out.println("private key d: " + ((BCECPrivateKey)kp.getPrivate()).getD());
        System.out.println("public key q:" + ((BCECPublicKey)kp.getPublic()).getQ()); //{x, y, zs...}

        BigInteger pris = ((BCECPrivateKey)kp.getPrivate()).getD();
        String privateKey99 = Hex.toHexString(pris.toByteArray());

        System.out.println("privateKey 16进制=:" + privateKey99);
        byte[] privateKey9 =Hex.decode(privateKey99);

        BigInteger userD = new BigInteger(1, privateKey9);
        System.out.println("privateKey99-1:" + userD);

        ECPoint ecPoint1 = ((BCECPublicKey)kp.getPublic()).getQ();
        String pubKey99 = Hex.toHexString(ecPoint1.getEncoded(false));
        byte buffer[] = ecPoint1.getEncoded(false);
        System.out.println("pubKey99 16进制公钥"+pubKey99);

        byte[] msg = "12345678message digest".getBytes();
        byte[] userId = "12345678".getBytes();
        byte[] sig = SM2Util.signSm3WithSm2(msg, userId, kp.getPrivate());
        System.out.println("签名="+Hex.toHexString(sig));
        System.out.println("验签结果="+SM2Util.verifySm3WithSm2(msg, userId, sig, kp.getPublic()));

        // 由d生成私钥 ---------------------
//        BigInteger d = new BigInteger("097b5230ef27c7df0fa768289d13ad4e8a96266f0fcb8de40d5942af4293a54a", 16);
//        BCECPrivateKey bcecPrivateKey = getPrivatekeyFromD(d);
        /*System.out.println(bcecPrivateKey.getParameters());
        System.out.println(Hex.toHexString(bcecPrivateKey.getEncoded()));

//        公钥X坐标PublicKeyXHex: 59cf9940ea0809a97b1cbffbb3e9d96d0fe842c1335418280bfc51dd4e08a5d4
//        公钥Y坐标PublicKeyYHex: 9a7f77c578644050e09a9adc4245d1e6eba97554bc8ffd4fe15a78f37f891ff8
//        PublicKey publicKey = getPublickeyFromX509File(new File("/Users/xxx/Downloads/xxxxx.cer"));
//        System.out.println(publicKey);getPublickeyFromXY
//        PublicKey publicKey1 = getPublickeyFromXY(new BigInteger("59cf9940ea0809a97b1cbffbb3e9d96d0fe842c1335418280bfc51dd4e08a5d4", 16), new BigInteger("9a7f77c578644050e09a9adc4245d1e6eba97554bc8ffd4fe15a78f37f891ff8", 16));
////        System.out.println(publicKey1);

        /*KeyPair kp1 = generateKeyPair();
        PublicKey publicKey2 = kp1.getPublic();
        PrivateKey privateKey2 = kp1.getPrivate();
        byte[]bs = sm2Encrypt("12345678".getBytes(), publicKey2);

        System.out.println("加密结果="+ Hex.toHexString(bs));
        bs = sm2Decrypt(bs, privateKey2);
        System.out.println("解密密结果="+ new String(bs));*/

        String pubKey991 = "04ed4034776089e6c5e46d9cffaaceec7931fcba8078f3ce4be3af6239dd7c45a791a3420f60a4694125938ab9242246e5e438293c297e7e237e699c9a6a7c290d";
        BigInteger priKey99 = new BigInteger("107332671644660149949936234419167643969871693807042484484154609745438997210936");
        BCECPublicKey kk = SM2Util.getPublickey(pubKey991);
        byte[]bs1 = SM2Util.sm2Encrypt("12345678".getBytes(), kk);
        System.out.println("加密结果="+ Hex.toHexString(bs1));

        BCECPrivateKey privateKey21 = SM2Util.getPrivatekeyFromD(priKey99);
        byte[] debs= SM2Util.sm2Decrypt(bs1, privateKey21);
        System.out.println("解密密结果="+ new String(debs));
    }

    @org.junit.jupiter.api.Test
    void signSm3WithSm2Asn1Rs() {
    }

    @org.junit.jupiter.api.Test
    void verifySm3WithSm2() {
    }

    @org.junit.jupiter.api.Test
    void verifySm3WithSm2Asn1Rs() {
    }

    @org.junit.jupiter.api.Test
    void sm2Decrypt() {
    }

    @org.junit.jupiter.api.Test
    void sm2Encrypt() {
    }

    @org.junit.jupiter.api.Test
    void sm2EncryptOld() {
    }

    @org.junit.jupiter.api.Test
    void sm2DecryptOld() {
    }

    @org.junit.jupiter.api.Test
    void sm4Encrypt() {
    }

    @org.junit.jupiter.api.Test
    void sm3() {
    }

    @org.junit.jupiter.api.Test
    void generateKeyPair() {
    }

    @org.junit.jupiter.api.Test
    void getPrivatekeyFromD() {
    }

    @org.junit.jupiter.api.Test
    void getPublickeyFromXY() {
    }

    @org.junit.jupiter.api.Test
    void getPublickey() {
    }

    @org.junit.jupiter.api.Test
    void getPublickeyFromX509File() {
    }
}